package com.java110.mall.goods.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.po.distributionMode.DistributionModePo;
import com.java110.mall.goods.bmo.distributionMode.IDeleteDistributionModeBMO;
import com.java110.mall.goods.bmo.distributionMode.IGetDistributionModeBMO;
import com.java110.mall.goods.bmo.distributionMode.ISaveDistributionModeBMO;
import com.java110.mall.goods.bmo.distributionMode.IUpdateDistributionModeBMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/distributionMode")
public class DistributionModeApi {

    @Autowired
    private ISaveDistributionModeBMO saveDistributionModeBMOImpl;

    @Autowired
    private IUpdateDistributionModeBMO updateDistributionModeBMOImpl;

    @Autowired
    private IDeleteDistributionModeBMO deleteDistributionModeBMOImpl;

    @Autowired
    private IGetDistributionModeBMO getDistributionModeBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /distributionMode/saveDistributionMode
     * @path /app/distributionMode/saveDistributionMode
     */
    @RequestMapping(value = "/saveDistributionMode", method = RequestMethod.POST)
    public ResponseEntity<String> saveDistributionMode(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "modeName", "请求报文中未包含modeName");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");


        DistributionModePo distributionModePo = BeanConvertUtil.covertBean(reqJson, DistributionModePo.class);
        distributionModePo.setbId("-1");
        return saveDistributionModeBMOImpl.save(distributionModePo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /distributionMode/updateDistributionMode
     * @path /app/distributionMode/updateDistributionMode
     */
    @RequestMapping(value = "/updateDistributionMode", method = RequestMethod.POST)
    public ResponseEntity<String> updateDistributionMode(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "modeId", "请求报文中未包含modeId");
        Assert.hasKeyAndValue(reqJson, "modeName", "请求报文中未包含modeName");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");


        DistributionModePo distributionModePo = BeanConvertUtil.covertBean(reqJson, DistributionModePo.class);
        return updateDistributionModeBMOImpl.update(distributionModePo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /distributionMode/deleteDistributionMode
     * @path /app/distributionMode/deleteDistributionMode
     */
    @RequestMapping(value = "/deleteDistributionMode", method = RequestMethod.POST)
    public ResponseEntity<String> deleteDistributionMode(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "shopId", "shopId不能为空");

        Assert.hasKeyAndValue(reqJson, "modeId", "modeId不能为空");


        DistributionModePo distributionModePo = BeanConvertUtil.covertBean(reqJson, DistributionModePo.class);
        return deleteDistributionModeBMOImpl.delete(distributionModePo);
    }

    /**
     * 微信删除消息模板
     *
     * @param shopId 小区ID
     * @return
     * @serviceCode /distributionMode/queryDistributionMode
     * @path /app/distributionMode/queryDistributionMode
     */
    @RequestMapping(value = "/queryDistributionMode", method = RequestMethod.GET)
    public ResponseEntity<String> queryDistributionMode(@RequestParam(value = "shopId", required = false) String shopId,
                                                        @RequestParam(value = "page") int page,
                                                        @RequestParam(value = "row") int row) {
        DistributionModeDto distributionModeDto = new DistributionModeDto();
        distributionModeDto.setPage(page);
        distributionModeDto.setRow(row);
        distributionModeDto.setShopId(shopId);
        return getDistributionModeBMOImpl.get(distributionModeDto);
    }
}
